﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace HuiLian.Cloud.Controls
{
    public class RoundProgressBar : ProgressBar
    {
        
        public RoundProgressBar()
        {
            this.ValueChanged += new RoutedPropertyChangedEventHandler<double>(progressBarValueChaged);
        }

        private void progressBarValueChaged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.ProgressBarValue = 360 * this.Value / this.Maximum ;
            this.ProgressBarText = this.Value + "秒";
        }

        public static readonly DependencyProperty ProgressBarColorProperty = DependencyProperty.Register("ProgressBarColor", typeof(Brush),
            typeof(RoundProgressBar), new PropertyMetadata(Brushes.LightGreen, OnPropertyChanged));

        /// <summary>
        /// 控件颜色
        /// </summary>
        public Brush ProgressBarColor
        {
            set { SetValue(ProgressBarColorProperty, value); }
            get { return (Brush)GetValue(ProgressBarColorProperty); }
        }

        public static readonly DependencyProperty ProgressBarThicknessProperty = DependencyProperty.Register("ProgressBarThickness", typeof(double),
            typeof(RoundProgressBar), new PropertyMetadata(10.0, OnPropertyChanged));

        /// <summary>
        /// 控件边宽
        /// </summary>
        public double ProgressBarThickness
        {
            set { SetValue(ProgressBarThicknessProperty, value); }
            get { return (double)GetValue(ProgressBarThicknessProperty); }
        }

        public static DependencyProperty ProgressBarValueProperty = DependencyProperty.Register("ProgressBarValue", typeof(double), typeof(RoundProgressBar),
            new FrameworkPropertyMetadata(0.0, new PropertyChangedCallback(OnPropertyChanged)));

        /// <summary>
        /// 进度条长度
        /// </summary>
        public double ProgressBarValue
        {
            set { SetValue(ProgressBarValueProperty, value); }
            get { return (double)GetValue(ProgressBarValueProperty); }
        }

        public static DependencyProperty ProgressBarTextProperty = DependencyProperty.Register("ProgressBarText", typeof(string), typeof(RoundProgressBar),
            new FrameworkPropertyMetadata("0秒", new PropertyChangedCallback(OnPropertyChanged)));

        /// <summary>
        /// 进度条文字
        /// </summary>
        public string ProgressBarText
        {
            set { SetValue(ProgressBarTextProperty, value); }
            get { return (string)GetValue(ProgressBarTextProperty); }
        }

        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RoundProgressBar progressBar = d as RoundProgressBar;
            if (e.Property.Name.Equals("ProgressBarColor"))
                progressBar.ProgressBarColor = (Brush)e.NewValue;
            if (e.Property.Name.Equals("ProgressBarThickness"))
                progressBar.ProgressBarThickness = (double)e.NewValue;
            if (e.Property.Name.Equals("ProgressBarValue"))
                progressBar.ProgressBarValue = (double)e.NewValue;
            if (e.Property.Name.Equals("ProgressBarText"))
                progressBar.ProgressBarText = (string)e.NewValue;
        }
    }
}
